<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 青 <99701759@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
// 微信用户表中的信息在$this->userinfo
// 用户newmember表的信息在$this->newmember中
// 用户角色表的信息在$this->role中
namespace app\web\controller;


use app\src\activity\model\UserActivity;
use app\src\goods\logic\ProductLogic;
use app\src\user\logic\MemberConfigLogic;
use app\src\user\logic\UserMemberLogic;

use app\src\wallet\logic\WalletHisLogicV2;
use app\src\wallet\logic\WalletLogic;
use app\src\activity\logic\ActivityLogic;
use app\src\activity\logic\UserActivityLogic;
use app\src\activity\logic\ActivitySignLogic;

use think\Db;

class ActSign extends Base{



    public function sign(){
        $memberinfo = $this->memberinfo;
        $uid = $memberinfo['uid'];
        $aid = $this->_param('aid');
//dump($uid.''.$aid);
        $join=(new UserActivityLogic())->getInfo(['uid'=>$uid,'act_id'=>$aid]);
        if($join['status']&&!empty($join['info'])){
            $activity=(new ActivityLogic())->getInfo(['id'=>$aid]);
            $this->issign($uid,$aid);
            $points=$activity['info']['integral'];

            Db::startTrans();
            $flag=false;
            $wallet=(new WalletLogic())->getInfo(['uid'=>$uid]);
            $last_points=$wallet['info']['points']+$points;
            $wallet_enity=['points'=>$last_points];
            $miu=(new WalletLogic())->save(['uid'=>$uid],$wallet_enity);
            $miu_log_info=[
                'uid'=>$uid,
                'before_points'=>$wallet['info']['points'],
                'points_type'=>'1',
                'plus'=>$points,
                'minus'=>'0',
                'after_money'=>$last_points,
                'create_time'=>time(),
                'reason'=>'签到',
                'dtree_type'=>0,
                'wallet_type'=>'0'
            ];
            $miu_log=(new WalletHisLogicV2())->add($miu_log_info);
            $sign=(new ActivitySignLogic())->add(['aid'=>$aid,'uid'=>$uid]);

            if(!$miu['status']) $flag=false;
            if(!$miu_log)$flag=false;
            if(!$sign['status']) $flag=false;
            if ($flag) {
                Db::rollback();
                $this->error('对不起 签到失败',url('user/index'));
            } else {
                Db::commit();
                $this->success('签到成功', url('user/index'));
            }

        }else{
            $this->error('未报名用户',url('user/index'));
        }


    }

    public function issign($uid,$aid){

        $map=array(
            'uid'=>$uid,
            'aid'=>$aid
        );
        $is_sign=(new ActivitySignLogic())->getInfo($map);
//dump($is_sign);exit;
        if($is_sign['status']&&!empty($is_sign['info'])){

           if($is_sign['info']['create_time']>10) {
               $this->error('已签到,不要重复签到',url('user/index'));
            }else{
                return true;
            }
        }

    }



}